package com.liuh.appsys.test;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/**
 * @note myBatis 工具类
 * @author liuh
 * */
public class BatisUtil {

	static SqlSessionFactory ssf = null;
	static {
		// 读取mybatis配置文件
		try {
			InputStream is = Resources.getResourceAsStream("my-batis1.xml");
			ssf = new SqlSessionFactoryBuilder().build(is);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @note 创建sqlSession
	 * @author liuh
	 * */
	public static SqlSession creatSqlSession() {
		return ssf.openSession();
	}

	/**
	 * @note 关闭sqlSeesion
	 * @author liuh
	 * */
	public static void closeSqlSession(SqlSession session) {
		if (null != session) {
			session.close();
		}
	}
}
